/*
* 定义 tokens
* 将模板字符串变为数组
* */
import Scanner from './Scanner.js'
import nestTokens from './nestTokens'

export default function changeAsTokens(templateStr){
    var scanner = new Scanner(templateStr)

    var tokens =[]
    var words;

    // 判断指针是否到头
    while (!scanner.eos()) {
        // 收集标记出现前的文字
        words = scanner.scanUtil('{{')
        if (words !==''){
            tokens.push(['text',words])
        }
        //跳过 {{
        scanner.scan('{{')
        //  收集标记出现前的文字
        words = scanner.scanUtil('}}')
        if (words !==''){
            if (words[0] === '#'){
                tokens.push(['#',words.substring(1)])
            }else if (words[0] === '/'){
                tokens.push(['/',words.substring(1)])
            }else {
                tokens.push(['name',words])
            }
        }
        //   跳过 }}
        scanner.scan('}}')
    }
    return nestTokens(tokens)
}
